﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecisionFramework.Text
{
    /// <summary>
	/// Provides helper methods for dealing with text strings. 
	/// </summary>
	/// <remarks>  
    /// Needs to be made culture aware.
    /// 
	/// Author: Greg Cowin
	/// </remarks> 
    public class TextHelper
    {
        /// <summary>
        /// Returns the given value with the appropriate singular or plural form of the
        /// description.
        /// </summary>
        /// <param name="val"></param>
        /// <param name="singular"></param>
        /// <param name="plural"></param>
        /// <returns></returns>
        public static String SingularPlural(long val, String singular, String plural)
        {
            return (val + " " + (val == 1 ? singular : plural));
        }

        /// <summary>
        /// Returns how long the time span represents in a readable form.
        /// </summary>
        /// <param name="timespan">The timespan to articulate.</param>
        /// <returns>A readable form of how long the given timespan represents.</returns>
        public static string HowLong(TimeSpan timespan)
        {
            if (timespan.Ticks == 0)
                return "less than 100 nanoseconds";

            StringBuilder builder = new StringBuilder();
            long days = timespan.Days;
            long hours = timespan.Hours % 24;
            long minutes = timespan.Minutes % 60;
            long seconds = timespan.Seconds % 60;
            long milliseconds = timespan.Milliseconds % 1000;
            long nanoseconds = (timespan.Ticks % (10000)) * 100;
            if (days > 0)
            {
                builder.Append(SingularPlural(days, "day", "days"));
            }
            if (hours > 0)
            {
                builder.Append(" ");
                builder.Append(SingularPlural(hours, "hour", "hours"));
            }
            if (minutes > 0)
            {
                builder.Append(" ");
                builder.Append(SingularPlural(minutes, "minute", "minutes"));
            }
            if (seconds > 0)
            {
                builder.Append(" ");
                builder.Append(SingularPlural(seconds, "second", "seconds"));
            }
            if (milliseconds > 0 || nanoseconds > 0)
            {

                builder.Append(" ");
                builder.Append(milliseconds
                    + ((double)nanoseconds / 1000000));
                builder.Append(" milliseconds");
            }

            return builder.ToString();
        }

        public static string Pluralize(string s, long number)
        {
            if (number == 1)
                return s;
            if (s.EndsWith("s"))
                return s.Substring(0, s.Length - 1) + "'";
            if (s.EndsWith("y"))
                return s.Substring(0, s.Length - 1) + "ies";
            return s + "s";
        }

        public static string UpTo(string source, int upToNumberOfCharacters)
        {
            if (upToNumberOfCharacters > source.Length)
                return source;
            else
                return source.Substring(0, upToNumberOfCharacters);
        }
    }
}
